package com.fg.cloud.factory.controller.product;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.fg.cloud.common.PageParamUtil;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.construction.CategoryTree;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.product.ProductCategoryEntity;
import com.fg.cloud.manager.service.product.ProductCategoryService;

/**
 * Description:商品分类
 * @author 陈银龙
 * @date 2017年8月11日下午4:22:24
 */
@Controller
@RequestMapping(value="/category")
public class ProductCategoryController extends BaseController{
	@Autowired
	private ProductCategoryService categoryInterface ;
	
	/**
	 * @Description: 通过ID查询商品分类信息 
	 * @param req
	 * @param resp
	 * @param id  商品分类Id
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月20日下午2:48:06
	 * @throws 
	 *
	 */
	@RequestMapping(value="/find",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void select(HttpServletRequest req , HttpServletResponse resp , @Param("id")Long id){
		if(id==null || id<=0){
			doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
			return;
		}
		try {
			ProductCategoryEntity categoryEntity = categoryInterface.find(id);
			if(categoryEntity != null){
				doResult(resp, true, StatusCode.FINDSUCCESS, categoryEntity, ResultType.FIND_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.FINDSUCCESS, categoryEntity, ResultType.FIND_NULL);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 新增商品分类信息 
	 * @param req
	 * @param resp
	 * @param categoryEntity  商品分类信息实体
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月20日下午2:52:59
	 * @throws 
	 *
	 */
	@RequestMapping(value="/insert",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void insert(HttpServletRequest req , HttpServletResponse resp , ProductCategoryEntity categoryEntity){
		try {
			categoryEntity.setFacId(getCurrentFactoryUser().getFacId());
			categoryEntity.setCreator(getCurrentFactoryUser().getId());
			if (categoryEntity == null || StringUtils.isEmpty(categoryEntity.getName())){
				doResult(resp, false, StatusCode.LACKVALUE, null, "分类名不能为空");
			}else{
				
				// 名称校验
				int num = categoryInterface.findnum(categoryEntity);
				if(num > 0){
					doResult(resp, false, StatusCode.SAVEERROR, null, "分类名称已存在");
					return;
				}
				int flag = categoryInterface.insert(categoryEntity);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.ADD_SUCCESS);
				}else{
					doResult(resp, false, StatusCode.SAVEERROR, flag, ResultType.ADD_FAULT);
				}
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 修改分类信息 
	 * @param req
	 * @param resp
	 * @param categoryEntity 商品分类信息实体
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月20日下午2:58:32
	 * @throws 
	 */
	@RequestMapping(value="/modify",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void update(HttpServletRequest req , HttpServletResponse resp, ProductCategoryEntity categoryEntity){
		try {
			if(categoryEntity==null || categoryEntity.getId()==null || categoryEntity.getId() < 1 ){
				doResult(resp, false, StatusCode.LACKVALUE, null, ResultType.INPUT_ERROR);
			}else{
				categoryEntity.setFacId(getCurrentFactoryUser().getFacId());
				categoryEntity.setUpdater(getCurrentFactoryUser().getId());
				
				// 名称校验
				int num = categoryInterface.findnum(categoryEntity);
				if(num > 0){
					doResult(resp, false, StatusCode.SAVEERROR, null, "分类名称已存在");
					return;
				}
				int flag = categoryInterface.updates(categoryEntity);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
				}else{
					doResult(resp, false, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
				}
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 根据ID删除商品分类信息 
	 * @param req
	 * @param resp
	 * @param id 
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月20日下午3:07:09
	 * @throws 
	 *
	 */
	@RequestMapping(value="/delete",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public void delete(HttpServletRequest req , HttpServletResponse resp , @Param("id")Long id){
		try {
			if(id == null || id <= 0 ){
				doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
			}else{
				Long facId = getCurrentFactoryUser().getFacId();
				Map<String, Object> condition = new HashMap<String, Object>();
				condition.put("id", id);
				condition.put("facId", facId);
				int flag = categoryInterface.delete(condition);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
				}else{
					doResult(resp, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
				}
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.REMOVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 条件查询商品分类信息 
	 * @param req
	 * @param resp
	 * @param parentId 
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月20日下午3:18:21
	 * @throws 
	 *
	 */
	@RequestMapping(value="/selectpage",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectPage(HttpServletRequest req , HttpServletResponse resp , @Param("parentId")Long parentId){
		try {
			Map<String,Object> params = PageParamUtil.pageParam(req, "parentId");
			PageUtils<ProductCategoryEntity> list = categoryInterface.selectPage(params);
			doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 通过父类ID查询子类信息 
	 * @param req
	 * @param resp
	 * @param parentId 
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月20日下午3:45:29
	 * @throws 
	 *
	 */
	@RequestMapping(value="/selectbyparentid",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectByParentId(HttpServletRequest req , HttpServletResponse resp , @Param("parentId")Long parentId){
		try {
			List<ProductCategoryEntity> categoryList = categoryInterface.selectByParentId(parentId);
			if(categoryList.size()>0){
				doResult(resp, true, StatusCode.FINDSUCCESS, categoryList, ResultType.FIND_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.FINDSUCCESS, categoryList, ResultType.FIND_NULL);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 级联删除商品分类 
	 * @param req
	 * @param resp
	 * @param parentId 
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月21日下午2:19:05
	 * @throws 
	 *
	 */
	@RequestMapping(value="/deletes",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void deleteByCategoryNumber(HttpServletRequest req , HttpServletResponse resp , Long id, String number){		
		try {
			if(id == null || id <= 0 || number == null){
				doResult(resp, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
			}else{
				String cnumber = number+"_"+id;
				Long facId = getCurrentFactoryUser().getFacId();
				
				Map<String, Object> condition = new HashMap<String, Object>();
				condition.put("number", cnumber);
				condition.put("facId", facId);
				condition.put("id", id);
				int flag =categoryInterface.deleteByCategoryNumber(condition);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
				}else if(flag == -1){
					doResult(resp, false, StatusCode.REMOVEERROR, null, "该类别或者子类别有商品，不能删除该类别");
				}else{
					doResult(resp, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
				}
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.REMOVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:查询全部数据并组成树形结构
	 * @param req
	 * @param resp
	 * @param facid
	 * @author 陈银龙
	 * @date 2017年8月9日下午2:41:33
	 */
	@RequestMapping(value="/selectByFacId",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectByFacId(HttpServletRequest req , HttpServletResponse resp){
		try {
			Long facId = getCurrentFactoryUser().getFacId();
			CategoryTree tree = categoryInterface.selectByFacId(facId);
			CategoryTree[] tree1 = new CategoryTree[]{tree};
			if (tree != null) {
				doResult(resp, true, StatusCode.FINDSUCCESS, tree1, ResultType.FIND_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.FINDSUCCESS, tree1, ResultType.FIND_NULL);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
}
